<template>
  <div>
    <van-nav-bar title="登录页面">
      <template #left>
        <van-icon
          name="arrow-left"
          size=".6rem"
          @click="$router.back()"
          color="grey"
        />
      </template>
      <template #right>
        <span @click="$router.replace('/')">首页</span>
      </template>
    </van-nav-bar>

    <div class="loginLogo">
      <img src="@/assets/icon/homeon.png" />
    </div>

    <van-form @submit="onSubmit">
      <van-field
        v-model="form.username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="form.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
        <hr />
        <van-button round block @click="$router.replace('/regist')"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    ...mapActions("user", ["login"]),
    onSubmit() {
      this.login(this.form)
        .then(() => {
          this.$toast({
            type: "success",
            message: "登录成功",
            onClose: () => {
              this.$router.back();
            },
          });
        })
        .catch((err) => {
          this.$toast({
            type: "fail",
            message: `${err.msg}`,
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.loginLogo img {
  display: block;
  margin: 1.066667rem auto;
}
</style>